package NiuKe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * 给定一个只包含小写字母字符串，每次可以选择两个相同的字符删除，并在字符串结尾新增任意一个小写字母。
 * 请问最少多少次操作后，所有的字母都不相同？
 * <p>
 * abab
 */
public class XiaoXiaoLe {
    public static void main(String[] args) {
        int result = minOperations2("nvqoynoaqyyqaoaoayvooonqovvonyyyavvnvaoqyavaovqanyqvaannoyyyqqoonvvqaqoqnv");
//        int result2 = minOperations("nvqoynoaqyyqaoaoayvooonqovvonyyyavvnvaoqyavaovqanyqvaannoyyyqqoonvvqaqoqnv");
        System.out.println(result);
//        System.out.println(result2);
    }

    public static int minOperations(String str) {
        int result = 0;
        ArrayList<Integer> keyList = new ArrayList<>(str.length());
        for (int i = 0; i < str.length(); i++) {
            keyList.add(i);
        }
        for (int i = 0; i < keyList.size(); i++) {
            if (keyList.get(i) < 0) {
                continue;
            }
            if (i + 1 >= keyList.size()) {
                break;
            }
            for (int j = i + 1; j < keyList.size(); j++) {
                if (keyList.get(j) < 0) {
                    continue;
                }
                Integer val = str.charAt(keyList.get(i)) ^ str.charAt(keyList.get(j));
                if (val == 0) {
                    result++;
                    keyList.set(i, -1);
                    keyList.set(j, -1);
                    break;
                }
            }
        }
        int count = 0;
        for (Integer key : keyList) {
            if(key > 0){
                count++;
            }
        }
        return result + count > 26 ? result + (result + count - 26) : result;
    }

    public static int minOperations2(String str) {
        HashMap<Character, Integer> wordsCountMap = new HashMap<>();
        for (char c : str.toCharArray()) {
            if (wordsCountMap.containsKey(c)) {
                Integer count = wordsCountMap.get(c);
                count+=1;
                wordsCountMap.put(c,count);
            }else {
                wordsCountMap.put(c,1);
            }
        }
        Integer result = 0;
        Integer remaind = 0;
        for (Map.Entry<Character, Integer> entry : wordsCountMap.entrySet()) {
            System.out.println("Entry key and value are " + entry.getKey() + " and " + entry.getValue());
            Character key = entry.getKey();
            Integer value = entry.getValue();
            int a = value / 2;
            int b = value % 2;
            result += a;
            remaind += b;
        }
        System.out.println(result);
        System.out.println(remaind);
        return result + remaind > 26 ? result + (result + remaind - 26) : result;
    }
}
